package com.k.kpp.dstest.controller;

import com.k.kpp.config.datasource.DynamicDataSourceContextHolder;
import com.k.kpp.config.datasource.DynamicDataSource;
import com.k.kpp.config.datasource.annotation.DataSourceSwitch;
import com.k.kpp.dstest.service.UserService;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author k
 */
@Controller
public class UserController {
    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userServiceImpl;
    @Autowired
    private DynamicDataSource dynamicDataSource;

    @RequestMapping("/user")
    public String userIndex(){
        LOGGER.debug("/user");
        return "user";
    }

    /**
     * 默认数据源
     * @return
     */
    @RequestMapping("/user/list")
    @ResponseBody
    public List userList(){
        LOGGER.debug("/user/list inParams:{}");
        return userServiceImpl.selectAll();
    }

    /**
     * master 数据源
     * @return
     */
    @DataSourceSwitch(value = "master")
    @RequestMapping("/user/listds1")
    @ResponseBody
    public List userList1(){
        LOGGER.debug("/user/listds1 inParams:{}");
        return userServiceImpl.selectAll();
    }

    /**
     * slave 数据源
     * @return
     */
    @DataSourceSwitch(value = "slave")
    @RequestMapping("/user/listds2")
    @ResponseBody
    public List userList2(){
        LOGGER.debug("/user/listds2 inParams:{}");
        return userServiceImpl.selectAll();
    }

    /**
     * 自定义 数据源
     * @return
     */
    @RequestMapping("/user/listds3")
    @ResponseBody
    public List userList3(){
        LOGGER.debug("/user/listds3 inParams:{}");
        List list = null;
        String lookupkey = "dsljw";
        return list;
    }
}
